﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using EPiServer;
using EPiServer.Core;
using System.Reflection;
using EPiServer.DataAbstraction;

namespace EPiSugar.UnitTesting
{
    /// <summary>
    /// Public accessor interface unit test faking funtionality.
    /// </summary>
    public static class Fakes
    {
        public static int RootPageTypeID = 1;
        public static int WastebasketTypeID = 2;
        public static int StartPageTypeID = 3;
        public static int NormalPageTypeID = 4;

        public static ShortCircuit CurrentCircuit { get; set; }
        public static FakePageProvider CurrentPageProvider { get; set; }

        public static void Initialize(FakeStructureBuilder builder)
        {
            ShortCircuit.EnsureConfiguration();
            CurrentCircuit = new ShortCircuit();
            CurrentPageProvider = new FakePageProvider();

            CurrentCircuit.InitializeFromConfiguration();
            builder.Build(CurrentPageProvider);

            // some trickery that can be avoided with the right license type
            var providerMap = (Dictionary<string, PageProviderBase>)typeof (PageProviderMap).GetField("_pageProviders", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(DataFactory.Instance.ProviderMap);
            providerMap[""] = CurrentPageProvider;

            CurrentPageProvider.Initialize("Fake", new NameValueCollection(), PageType.List());
        }
    }
}
